package com.tdcy.biz.wxmgt.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
import me.chanjar.weixin.mp.bean.tag.WxTagListUser.WxTagListUserData;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tdcy.biz.wxmgt.service.WeixinService;
import com.tdcy.biz.wxmgt.service.bean.UserTagBean;
import com.tdcy.framework.BaseController;
import com.tdcy.framework.util.StringUtils;
import com.tdcy.framework.util.WebUtils;

/**
*/
@Controller
@RequestMapping(value = "/wxusertag")
public class WxUserTagController extends BaseController {
	@Autowired
	WeixinService wxMpService;

	@ResponseBody
	@RequestMapping(value = "/getUserTags", method = RequestMethod.POST)
	public Object getUserTags(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		List<WxUserTag> res = wxMpService.getUserTagService().tagGet();
		return WebUtils.createJSONSuccess("更新成功", res);
	}

	@ResponseBody
	@RequestMapping(value = "/updateUserTags", method = RequestMethod.POST)
	public Object updateUserTags(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		wxMpService.getUserTagService().tagUpdate(userTagBean.getId(),
				userTagBean.getCode());
		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/addUserTags", method = RequestMethod.POST)
	public Object addUserTags(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		wxMpService.getUserTagService().tagCreate(userTagBean.getCode());
		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/delUserTags", method = RequestMethod.POST)
	public Object delUserTags(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		wxMpService.getUserTagService().tagDelete(userTagBean.getId());
		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/tagListUser", method = RequestMethod.POST)
	public Object tagListUser(UserTagBean userTagBean) throws IOException,
			WxErrorException {

		List<WxMpUser> userList = handleTagListUser(userTagBean);

		return WebUtils.createJSONSuccess("成功", userList);
	}

	public List<WxMpUser> handleTagListUser(UserTagBean userTagBean)
			throws WxErrorException {
		List<WxMpUser> userList = new ArrayList<WxMpUser>();
		String nextOpenId = "";
		while (true) {
			WxTagListUser wxTagListUser = wxMpService.getUserTagService()
					.tagListUser(userTagBean.getId(), nextOpenId);

			WxTagListUserData data = wxTagListUser.getData();
			if (data != null) {
				List<String> openIds = data.getOpenidList();
				for (int i = 1; i <= openIds.size(); i++) {
					WxMpUser user = wxMpService.getUserService().userInfo(
							openIds.get(i - 1));
					userList.add(user);
				}
			}

			nextOpenId = wxTagListUser.getNextOpenid();
			if (StringUtils.isEmpty(nextOpenId)) {
				break;
			}
		}

		return userList;
	}

	@ResponseBody
	@RequestMapping(value = "/batchTagging", method = RequestMethod.POST)
	public Object batchTagging(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		wxMpService.getUserTagService().batchTagging(userTagBean.getId(),
				userTagBean.getOpenids().split(","));

		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/batchUntagging", method = RequestMethod.POST)
	public Object batchUntagging(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		wxMpService.getUserTagService().batchUntagging(userTagBean.getId(),
				userTagBean.getOpenids().split(","));

		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/userTagList", method = RequestMethod.POST)
	public Object userTagList(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		List<Long> tagIds = wxMpService.getUserTagService().userTagList(
				userTagBean.getOpenId());

		List<WxUserTag> tres = new ArrayList<WxUserTag>();
		List<WxUserTag> res = wxMpService.getUserTagService().tagGet();
		for (WxUserTag wxusertag : res) {
			for (Long id : tagIds) {
				if (id == wxusertag.getId()) {
					tres.add(wxusertag);
				}
			}
		}

		return WebUtils.createJSONSuccess("成功", tres);
	}
}
